/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-12-22 17:11:58
 * @LastEditors: turingchang
 * @LastEditTime: 2020-12-23 18:07:13
 */

'use strict';

const Service = require('egg').Service;
const { Op } = require("sequelize");
const sequelize = require('sequelize');

class JobLogService extends Service {   

    /**
     * 添加计划任务组.post
     */
    async add(params){
        const ctx =this.ctx;
        let job_group ={};
        let res = {
            success:false,
            msg:'',
            response_code_enum:null,
        }        
        if(!params){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }  
        if(!params.job_id){
            res.response_code_enum = ctx.app.enums.response_code_enum.miss_required_params;
            return res;
        }  
        if(!params.response_success || !ctx.app.utils.validator.isNum(params.response_success)){         
            params.response_success =0;
        } 
        params.uuid = ctx.app.utils.generator.uid();
        let save_result = await ctx.app.model.Job.JobLog.create(params);
        if(!save_result){
            res.msg = "添加失败";
            res.response_code_enum = ctx.app.enums.response_code_enum.server_error;
            return res;
        }          
        //更新job_id项的下次执行时间
        if(params.next_invocation){
            await ctx.service.job.job.edit({
                id:params.job_id,
                next_invocation:params.next_invocation
            });
        }        
        res.msg = "添加成功";
        res.success =true;
        return res;
    }  
  
}

module.exports = JobLogService;